//
//  AppDelegate.m
//  01-UIViewHomeWork
//
//  Created by QC.L on 16/7/14.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

#define SCREEN_WIDTH _window.frame.size.width

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor = [UIColor whiteColor];
    [_window makeKeyAndVisible];
    _window.rootViewController = [[ViewController alloc] init];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"welcome_2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [_window addSubview:imageView];
    imageView.frame = _window.frame;
    UIImageView *imageViewOfLaGouIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo_lagou"]];
    [imageView addSubview:imageViewOfLaGouIcon];
    imageViewOfLaGouIcon.frame = CGRectMake(0, 0, 92, 148);
    imageViewOfLaGouIcon.center = CGPointMake(SCREEN_WIDTH / 2, 240);
#pragma mark - 输入用户名
    UITextField *textFieldOfUser = [[UITextField alloc] initWithFrame:CGRectMake(14, 350, SCREEN_WIDTH - 28, 44)];
    [_window addSubview:textFieldOfUser];
    textFieldOfUser.backgroundColor = [UIColor blackColor];
    textFieldOfUser.alpha = 0.4;
    textFieldOfUser.textColor = [UIColor whiteColor];
    textFieldOfUser.placeholder = @"请输入用户名";
    textFieldOfUser.delegate = self;
    textFieldOfUser.tag = 3333;
#pragma mark - 用户框画圆角
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:textFieldOfUser.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = textFieldOfUser.bounds;
    maskLayer.path = maskPath.CGPath;
    textFieldOfUser.layer.mask = maskLayer;
#pragma mark - 用户图标
    UIButton *buttonOfUser = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonOfUser setImage:[UIImage imageNamed:@"icon_username"] forState:UIControlStateNormal];
    [_window addSubview:buttonOfUser];
    buttonOfUser.frame = CGRectMake(14, 350, 44, 44);
    buttonOfUser.backgroundColor = [UIColor blackColor];
    buttonOfUser.alpha = 0.8;
    textFieldOfUser.leftViewMode = UITextFieldViewModeAlways;
    textFieldOfUser.leftView = buttonOfUser;
    textFieldOfUser.leftView.backgroundColor = [UIColor blackColor];
    buttonOfUser.tag = 333;
#pragma mark - 输入密码
    UITextField *textFieldOfPassword = [[UITextField alloc] initWithFrame:CGRectMake(14, 395, SCREEN_WIDTH - 28, 44)];
    [_window addSubview:textFieldOfPassword];
    textFieldOfPassword.backgroundColor = [UIColor blackColor];
    textFieldOfPassword.alpha = 0.4;
    textFieldOfPassword.textColor = [UIColor whiteColor];
    textFieldOfPassword.delegate = self;
    textFieldOfPassword.tag = 6666;
    textFieldOfPassword.secureTextEntry = YES;
#pragma mark - 密码框画圆角
    UIBezierPath *maskPathP = [UIBezierPath bezierPathWithRoundedRect:textFieldOfUser.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayerP = [[CAShapeLayer alloc] init];
    maskLayerP.frame = textFieldOfUser.bounds;
    maskLayerP.path = maskPathP.CGPath;
    textFieldOfPassword.layer.mask = maskLayerP;
#pragma mark - 密码图标
    UIButton *buttonOfPassword = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonOfPassword setImage:[UIImage imageNamed:@"icon_password"] forState:UIControlStateNormal];
    [_window addSubview:buttonOfPassword];
    buttonOfPassword.frame = CGRectMake(14, 395, 44, 44);
    buttonOfPassword.backgroundColor = [UIColor blackColor];
    buttonOfPassword.alpha = 0.8;
    textFieldOfPassword.leftViewMode = UITextFieldViewModeAlways;
    textFieldOfPassword.leftView = buttonOfPassword;
    buttonOfPassword.tag = 666;
#pragma mark - 登录按钮
    UIButton *login = [UIButton buttonWithType:UIButtonTypeSystem];
    [login setTitle:@"登录" forState:UIControlStateNormal];
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_window addSubview:login];
    login.backgroundColor = [UIColor colorWithRed:0.01 green:0.81 blue:0.53 alpha:1.00];
    login.frame = CGRectMake(14, 450, SCREEN_WIDTH - 28, 44);
    login.layer.cornerRadius = 5;
    [login addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
#pragma mark - 忘记密码
    UIButton *buttonOfForget = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonOfForget setTitle:@"忘记密码" forState:UIControlStateNormal];
    buttonOfForget.titleLabel.textAlignment = NSTextAlignmentLeft;
    [buttonOfForget setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_window addSubview:buttonOfForget];
    buttonOfForget.frame = CGRectMake(10, 505, 67, 44);
#pragma mark - 现在注册拉勾
    UIButton *buttonOfAd = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonOfAd.frame = CGRectMake(0, 0, 150, 44);
    buttonOfAd.center = CGPointMake(SCREEN_WIDTH / 2, 570);
    [buttonOfAd setTitle:@"现在注册拉勾" forState:UIControlStateNormal];
    [buttonOfAd setTitleColor:[UIColor colorWithRed:0.01 green:0.64 blue:0.49 alpha:1.00] forState:UIControlStateNormal];
    [_window addSubview:buttonOfAd];
#pragma mark - or
    UIView *viewOfLineLeft = [[UIView alloc] initWithFrame:CGRectMake(14, 600, SCREEN_WIDTH / 2 - 30, 1)];
    UIView *viewOfLineRigt = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 + 14, 600, SCREEN_WIDTH / 2 - 30, 1)];
    viewOfLineLeft.backgroundColor = [UIColor whiteColor];
    viewOfLineRigt.backgroundColor = [UIColor whiteColor];
    [_window addSubview:viewOfLineLeft];
    [_window addSubview:viewOfLineRigt];
    UILabel *labelOfOr = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    labelOfOr.center = CGPointMake(SCREEN_WIDTH / 2, 600);
    labelOfOr.text = @"or";
    labelOfOr.textAlignment = NSTextAlignmentCenter;
    labelOfOr.textColor = [UIColor whiteColor];
    [_window addSubview:labelOfOr];
#pragma mark - 随便逛逛
    UIButton *buttonOfCasual = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonOfCasual setTitle:@"随便逛逛>" forState:UIControlStateNormal];
    [buttonOfCasual setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    buttonOfCasual.frame = CGRectMake(0, 0, 100, 44);
    buttonOfCasual.center = CGPointMake(SCREEN_WIDTH / 2, 650);
    [_window addSubview:buttonOfCasual];
    return YES;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [_window endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (void)handleButton:(UIButton *)button {
    [_window endEditing:YES];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 3333) {
        UIButton *user = [_window viewWithTag:333];
        user.selected = !user.selected;
        [user setImage:[UIImage imageNamed:@"icon_username_select"] forState:UIControlStateSelected];
    }
    if (textField.tag == 6666) {
        UIButton *pass = [_window viewWithTag:666];
        pass.selected = !pass.selected;
        [pass setImage:[UIImage imageNamed:@"icon_password_select"] forState:UIControlStateSelected];
    }
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (textField.tag == 3333) {
        UIButton *user = [_window viewWithTag:333];
        user.selected = !user.selected;
        [user setImage:[UIImage imageNamed:@"icon_username"] forState:UIControlStateSelected];
    }
    if (textField.tag == 6666) {
        UIButton *pass = [_window viewWithTag:666];
        pass.selected = !pass.selected;
        [pass setImage:[UIImage imageNamed:@"icon_password"] forState:UIControlStateSelected];
    }
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
